<?php

namespace app\modules\Product\models;

use Yii;
use app\modules\Manage\models\User;
use app\modules\Basic\models\CityCode;
/**
 * This is the model class for table "product_serve".
 *
 * @property int $id
 * @property int $product_id 团队ID
 * @property string $begin_date 开始日期
 * @property string $end_date 结束日期
 * @property string $city 目的地城市
 * @property string $member_id 指定导游(多个)
 * @property int $status 1正常  2 禁用 
 * @property int $user_id 创建人ID
 * @property string $created_at 创建时间
 * @property string $updated_at
 */
class ProductServe extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'product_serve';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['product_id', 'status', 'user_id'], 'integer'],
            [['begin_date', 'end_date','city','member_id'], 'required'],
            [['begin_date', 'end_date', 'created_at', 'updated_at','city', 'member_id'], 'safe'],
        ];
    }

       //保存前处理
    public function beforeSave($insert) {//{{{

        if($insert){

            $this->created_at = date("Y-m-d H:i:s");
            $this->user_id    = Yii::$app->user->id;
            $this->status = 1;
        }

        if(is_array($this->city)){
           $this->city = json_encode($this->city); 
        }

        if(is_array($this->member_id)){
           $this->member_id = json_encode($this->member_id); 
        }
        return parent::beforeSave($insert);

    }//}}}

     //  获取团队信息
    public function getProduct(){
        return $this->hasOne(Product::className(), ['id' => 'product_id']);
    }

    //获取
    public function getUserName(){
        $user = User::findOne($this->user_id);
        return $user ? $user->name : ' ';
    }

    //获取城市
    public function getCityInfo(){
        $city_str = '';
        if($this->city){
            $city = json_decode($this->city);
            foreach($city as $c){
                $city_m = CityCode::find()->where(['code'=>$c])->one();
                if($city_m){
                    $city_str .= $city_m->code.'-'.$city_m->zh_name.' / ';
                }
                
            }
        }
        return $city_str;
    }
    //获取服务经理
    public function getMemberInfo(){
        $mem_str = '';
        if($this->member_id){
            $member_id = json_decode($this->member_id);
            foreach($member_id as $c){
                $tour_m = User::find()->where(['id'=>$c])->one();
                if($tour_m){
                    $mem_str .= $tour_m->name.' / ';
                }
                
            }
        }
        return $mem_str;
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'product_id' => Yii::t('app', 'Product ID'),
            'begin_date' => Yii::t('app.tour', 'Begin Date'),
            'end_date' => Yii::t('app.tour', 'End Date'),
            'city' => Yii::t('app.tour', 'City'),
            'member_id' => Yii::t('app.tour', 'Member ID'),
            'status' => Yii::t('app', 'Status'),
            'user_id' => Yii::t('app', 'User ID'),
            'created_at' => Yii::t('app', 'Created At'),
            'updated_at' => Yii::t('app', 'Updated At'),
        ];
    }
}
